package com.tv.enoguia;

import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.tv.enoguia.Utils.CustomViewPager;
import com.tv.enoguia.Utils.TabIcon;
import com.tv.enoguia.fragment.FavourScr;
import com.tv.enoguia.fragment.ListScr;
import com.tv.enoguia.fragment.MapScr;

public class MainPanelActivity extends FragmentActivity implements OnClickListener, OnPageChangeListener{
	
	TabIcon tab1, tab2, tab3, tab4, tab5;
	
	CustomViewPager viewPager;
	MyAdapter adapter;
	String value;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_panel);
		
		tab1 = (TabIcon) findViewById(R.id.tab1);
		tab2 = (TabIcon) findViewById(R.id.tab2);
		tab3 = (TabIcon) findViewById(R.id.tab3);
		tab4 = (TabIcon) findViewById(R.id.tab4);
		
		
		String language = Locale.getDefault().getISO3Language().toString(); 
		
		if (language.equals("eng")){
			tab1.setImageResource(R.drawable.icon_home_l);
			tab2.setImageResource(R.drawable.icon_cellers_gray_l);
			tab3.setImageResource(R.drawable.icon_mapa_l);
			tab4.setImageResource(R.drawable.icon_favoritos_l);
		} else if (language.equals("cat")) {
			tab1.setImageResource(R.drawable.icon_home_ca);
			tab2.setImageResource(R.drawable.icon_cellers_gray_ca);
			tab3.setImageResource(R.drawable.icon_mapa_ca);
			tab4.setImageResource(R.drawable.icon_favoritos_ca);
		}else if (language.equals("spa")) {
			tab1.setImageResource(R.drawable.icon_home_sp);
			tab2.setImageResource(R.drawable.icon_cellers_gray_sp);
			tab3.setImageResource(R.drawable.icon_mapa_sp);
			tab4.setImageResource(R.drawable.icon_favoritos_sp);
		} else if (language.equals("vie")) {
			tab1.setImageResource(R.drawable.icon_home_ca);
			tab2.setImageResource(R.drawable.icon_cellers_gray_ca);
			tab3.setImageResource(R.drawable.icon_mapa_ca);
			tab4.setImageResource(R.drawable.icon_favoritos_ca);
		}
		
		
		tab1.setOnClickListener(this);
		tab2.setOnClickListener(this);
		tab3.setOnClickListener(this);
		tab4.setOnClickListener(this);
		
		viewPager = (CustomViewPager) findViewById(R.id.pager);
		adapter = new MyAdapter(getSupportFragmentManager());
		viewPager.setAdapter(adapter);
		viewPager.setOnPageChangeListener(this);
		viewPager.setPageMargin(2);
		
		viewPager.setPagingEnabled(false);
		
		
		value = getIntent().getStringExtra("value");
		
		Log.i("DATA", "VALUE : "+value+ "value: "+Integer.parseInt(value));
		
		setChecked(Integer.parseInt(value));
	}
	
	void init(){
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tab1:
			finish();
			break;
		case R.id.tab2:
			setChecked(0);
			break;
		case R.id.tab3:
			setChecked(1);
			break;
		case R.id.tab4:
			setChecked(2);
			break;
		default:
			break;
		}
	}
	
	
	class MyAdapter extends FragmentPagerAdapter {
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			//super.destroyItem(container, position, object);
		}

		public MyAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int i) {
			switch (i) {
			case 0:
				return new ListScr();
			case 1:
				return new MapScr();
			case 2:
				return new FavourScr();
			default:
				return new ListScr();
			}
		}

		@Override
		public int getCount() {
			return 3;
		}
		
	}
	
	private void setChecked(int id) {
		try {
			TabIcon[] tabs = {tab2, tab3, tab4};
			viewPager.setCurrentItem(id);
			for (int i = 0; i < tabs.length; i++) {
				if(id!=i) {
					tabs[i].setChecked(false);
				} else {
					tabs[i].setChecked(true);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPageSelected(int id) {
		setChecked(id);
	}
	
	@Override
	public void onBackPressed() {
	    Fragment fragment = (Fragment) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":"+viewPager.getCurrentItem());
	    if (fragment != null) // could be null if not instantiated yet
	    {
	        if (fragment.getView() != null) {
	            // Pop the backstack on the ChildManager if there is any. If not, close this activity as normal.
	            if (!fragment.getChildFragmentManager().popBackStackImmediate()) {
	                finish();
	            }
	        }
	    }
	}
}
